package ai4games_tests.soccer;

import game2dai.World;
import game2dai.entities.BaseEntity;
import game2dai.fsm.State;
import game2dai.fsm.Telegram;

public class PlayerWait extends State implements Constants{


	private static PlayerWait instance = null;

	public static PlayerWait state(){
		if(instance == null){
			instance = new PlayerWait();
			instance.name = "Wait";
		}
		return instance;
	}

	@Override
	public void enter(BaseEntity owner) {
		//PlayerBase player = (PlayerBase) owner;
	}

	@Override
	public void execute(BaseEntity owner, double deltaTime, World world) {
		PlayerBase player = (PlayerBase) owner;
		if(!player.isNearTarget()){
			player.AP().arriveOn();
			return;
		}
		player.AP().arriveOff();
		player.velocity(0,0);
		if(player.team.pitch.ballInPlay)
			player.trackBall();

		if(player.team.pitch.ballInPlay){
			// If our team is in control and the player is ahead of the attacker then ask for a pass
			if(player.team.isInControl() && !player.isControllingPlayer() && player.isAheadOfAttacker()){
				player.team.requestPass(player);
				return;
			}
			// if closest player to ball and there is no assigned receiver and 
			// neither goalkeeper has the ball then chase it
			if(player == player.team.getPlayerClosestToBall() && 
					player.team.getReceivingPlayer() == null &&
					!player.team.pitch.ball.keeperHasBall()){
				player.FSM().changeState(PlayerChaseBall.state());
				return;
			}
		}
	}

	@Override
	public void exit(BaseEntity owner) {
	}

	@Override
	public boolean onMessage(BaseEntity owner, Telegram tgram) {
		return false;
	}

}
